#include "h_cc2640r2f_gpio.h"
#include "h_cc2640r2f_define.h"

#include "h_cc2640r2f_uart.h"
//#include <board.h>

#include "h_cc2640r2f_spi.h"



#include <ti/sysbios/knl/Clock.h>
#include "util.h"


// Debounce timeout in milliseconds
#define H_CC2640R2F_GPIOInput_DEBOUNCE_TIMEOUT  200
////////////////////////////////////////////////////////////////////////////////
/*
    User define part
*/
const PIN_Config H_CC2640R2F_GPIO_OutputCFG[] = 
{
//    /* The Green LED on test board */
//    S_Lock_KeyBoard_GreenLED_Pin | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
//    /* The Read LED on test board */
//    S_Lock_KeyBoard_RedLED_Pin | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
//
//    S_Lock_KeyBoard_BackLED_Pin | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
//    /* The Board SPI CSN Interface */
//    H_CC2640R2F_SPI0IO | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
//    /* Status LED */
//    IOID_30 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
//
//    /* PSAM Power Supply */
//    S_PSAMCARD_POWERCONTRL_IO | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
//    /* PSAM Reset Control */
//    S_PSAMCARD_RST_IO | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL | PIN_DRVSTR_MAX,
//
//    //Motor A
//    S_LOCK_MOTOR_A | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
//    //MOTOR B
//    S_LOCK_MOTOR_B | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
    
    PIN_TERMINATE
};
static PIN_Handle H_CC2640R2F_GPIO_OutputHandle = NULL;
static PIN_State H_CC2640R2F_GPIO_OutputState;

const PIN_Config H_CC2640R2F_GPIO_InputCFG[] = 
{
    //KeyBoard IRQ Pin
//    S_Lock_KeyBoard_IRQ_Pin | PIN_GPIO_OUTPUT_DIS | PIN_INPUT_EN | PIN_NOPULL,
    
    PIN_TERMINATE
};
static PIN_Handle H_CC2640R2F_GPIO_InputHandle = NULL;
static PIN_State H_CC2640R2F_GPIO_InputState;
static Clock_Struct H_CC2640R2F_GPIO_InputChangeClock;
unsigned char H_CC2640R2F_GPIO_InputChange = 0;

H_CC2640R2F_GPIO_InputCB_t GPIOInputChangeHandler = NULL;
////////////////////////////////////////////////////////////////////////////////
static void H_CC2640R2F_GPIO_InputCallback(PIN_Handle hPin, PIN_Id pin_Id)
{
    Util_startClock(&H_CC2640R2F_GPIO_InputChangeClock);
}

static void H_CC2640R2F_GPIO_InputChangeHandler(UArg a0)
{
    if (GPIOInputChangeHandler != NULL)
    {
        //Get a negative edge from this pin
//        if(0 == PIN_getInputValue(S_Lock_KeyBoard_IRQ_Pin))
//        {
//            H_CC2640R2F_GPIO_InputChange = 1;
//        }
        
        // Notify the application
        (*GPIOInputChangeHandler)(H_CC2640R2F_GPIO_InputChange);
    }
}


//signed char H_CC2640R2F_GPIO_InputInit(H_CC2640R2F_GPIO_InputCB_t GPIOChangeCB)
//{
//    H_CC2640R2F_GPIO_InputHandle = PIN_open(&H_CC2640R2F_GPIO_InputState, \
//                                                H_CC2640R2F_GPIO_InputCFG);
//    PIN_registerIntCb(H_CC2640R2F_GPIO_InputHandle, \
//                        H_CC2640R2F_GPIO_InputCallback);
//    PIN_setConfig(H_CC2640R2F_GPIO_InputHandle, PIN_BM_IRQ, \
//                    S_Lock_KeyBoard_IRQ_Pin | PIN_IRQ_NEGEDGE);
//
//    //Setup input gpio callback
//    Util_constructClock(&H_CC2640R2F_GPIO_InputChangeClock, \
//                          H_CC2640R2F_GPIO_InputChangeHandler, \
//                            H_CC2640R2F_GPIOInput_DEBOUNCE_TIMEOUT, \
//                              0, false, 0);
//
//    GPIOInputChangeHandler = GPIOChangeCB;
//
//    return 0;
//}

/*
    Init the Ouput GPIO list by TI official system function.
*/
signed char H_CC2640R2F_GPIO_OutputInit(void)
{
    if(NULL != H_CC2640R2F_GPIO_OutputHandle)
    {
        return (-1);
    }
    H_CC2640R2F_GPIO_OutputHandle = PIN_open(&H_CC2640R2F_GPIO_OutputState, \
                                                H_CC2640R2F_GPIO_OutputCFG);
    return 0;
}

/*
    Set the Output GPIO value.
*/
signed char H_CC2640R2F_GPIO_OutputSet(unsigned char PinID, unsigned int value)
{
    if(NULL == H_CC2640R2F_GPIO_OutputHandle)
    {
        return (-1);
    }
    PIN_setOutputValue(H_CC2640R2F_GPIO_OutputHandle, PinID, value);
    return 0;
}

/*
    Toogle the Output GPIO
*/
/*
signed char H_CC2640R2F_GPIO_OutputToggle(unsigned char PinID)
{
    if(NULL == H_CC2640R2F_GPIO_OutputHandle)
    {
        return (-1);
    }
    PIN_setOutputValue(H_CC2640R2F_GPIO_OutputHandle, PinID, \
                        ~PIN_getOutputValue(PinID));
                        //sizeof(uint32_t) - PIN_getOutputValue(PinID));
    //H_CC2640R2F_UART_Debug("The IO(%d) is %d\r\n", PinID, PIN_getOutputValue(PinID));
    
    return 0;
}
*/

/*
    This is a function which will set the GPIO mode, but the TI just releases 
    PIN_open() function and I didn't find the GPIO control register address.
    So this function keeps null.
*/
/*
signed char H_CC2640R2F_GPIO_Init(unsigned int PiNum, unsigned int PinState, \
                                    unsigned int PinInitState, \
                                      unsigned int PinDriveMode, \
                                        unsigned int PinDrivePower)
{
    return 0;
}
*/


